home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / timer / sun3.md / timerTick.c < prev    next >
C/C++ Source or Header  |  1990-11-08  |  8KB  |  335 lines

  1. /*
  2.  * timerTick.c --
  3.  *
  4.  *    Kernel utility procedures to manipulate time Tick values for the Sun3.
  5.  *
  6.  *  The routines in this module manipulate time values that are
  7.  *  represented in the Timer_Ticks format.  The Timer_Ticks format is used
  8.  *  for a specific purpose: to make the operations associated with the
  9.  *  callback timer and timer queue run fast. These operations include
  10.  *  starting the timer, scheduling a routine and calling a routine at its
  11.  *  scheduled time.  Unlike the Time format, which represents time in
  12.  *  seconds and microseconds, the Timer_Ticks format represents time in a
  13.  *  machine-dependent way. On the Sun-3, the hardware free-running
  14.  *  counter format is easily converted to the Time format, so no
  15.  *  distinction is made between Time and Timer_Ticks.
  16.  *
  17.  *
  18.  *  There are several constraints imposed on the Timer_Ticks format to
  19.  *  decrease complexity and overhead in using the format.  First, it can
  20.  *  not be used to represent negative time values.  Second, the routines
  21.  *  are not general. For example, there are no multiply and divide
  22.  *  routines for Timer_Ticks values.  Full generality is obtained by using
  23.  *  the Time module.
  24.  *
  25.  * Copyright 1986, 1988 Regents of the University of California
  26.  * Permission to use, copy, modify, and distribute this
  27.  * software and its documentation for any purpose and without
  28.  * fee is hereby granted, provided that the above copyright
  29.  * notice appear in all copies.  The University of California
  30.  * makes no representations about the suitability of this
  31.  * software for any purpose.  It is provided "as is" without
  32.  * express or implied warranty.
  33.  */
  34.  
  35. #ifndef lint
  36. static char rcsid[] = "$Header: /sprite/src/kernel/timer/sun3.md/RCS/timerTick.c,v 9.1 90/09/06 18:17:47 jhh Exp $ SPRITE (Berkeley)";
  37. #endif /* not lint */
  38.  
  39. #include "sprite.h"
  40. #include "mach.h"
  41. #include "timerTick.h"
  42. #include "spriteTime.h"
  43. #include "timerIntersilInt.h"
  44. #include "sys.h"
  45. #include "bstring.h"
  46. #include "stdio.h"
  47.  
  48.  
  49. /*
  50.  *  Definition of the maximum number of seconds and microseconds that the
  51.  *  hardware free-running counter can count to.  To make intervals fit in
  52.  *  a 32-bit integer, they are constrained to be less than or equal to
  53.  *  timer_MaxIntervalTime.seconds. To use intervals greater than this
  54.  *  value, one must use the Time_ module arithmetic routines.
  55.  */
  56.  
  57. Time timer_MaxIntervalTime;
  58.  
  59. /*
  60.  * Some commonly used values for intervals.
  61.  */
  62.  
  63. Timer_Ticks    timer_TicksZeroSeconds;
  64. unsigned int    timer_IntZeroSeconds;
  65. unsigned int    timer_IntOneSecond;
  66. unsigned int    timer_IntOneMillisecond;
  67. unsigned int    timer_IntOneMinute;
  68. unsigned int    timer_IntOneHour;
  69.  
  70.  
  71. /*
  72.  * The interval value to represent one second. It must be at least 1000
  73.  * so that one milliscond can be be represented in an interval.
  74.  */
  75.  
  76. #define ONE_SEC_INTERVAL    1000
  77.  
  78.  
  79. /*
  80.  * The maximum amount of time that an interval can represent.
  81.  */
  82.  
  83. #define MAXINT ((unsigned int) 0xFFFFFFFF)
  84. static Time maxIntervalTime = {
  85.     MAXINT / ONE_SEC_INTERVAL,
  86.     ((MAXINT % ONE_SEC_INTERVAL) * ONE_SECOND) / ONE_SEC_INTERVAL,
  87. };
  88.  
  89.  
  90. /*
  91.  * Forward declaration of routines.
  92.  */
  93. static void ConvertTimeToInt _ARGS_((Time time, unsigned int *resultPtr));
  94. static void ConvertIntToTime _ARGS_((unsigned int counter, Time *resultPtr));
  95.  
  96.  
  97. /*
  98.  *----------------------------------------------------------------------
  99.  *
  100.  * TimerTicksInit --
  101.  *
  102.  *    Initializes the various tick and interval values.
  103.  *
  104.  * Results:
  105.  *    None.
  106.  *
  107.  * Side effects:
  108.  *    None.
  109.  *
  110.  *----------------------------------------------------------------------
  111.  */
  112.  
  113. void
  114. TimerTicksInit()
  115. {
  116.     Time tmp;
  117.  
  118.     ConvertIntToTime((unsigned int) 0xFFFFFFFF, &timer_MaxIntervalTime);
  119.  
  120.     tmp.seconds = 1;
  121.     tmp.microseconds = 0;
  122.     ConvertTimeToInt(tmp, &timer_IntOneSecond);
  123.  
  124.     tmp.seconds = 0;
  125.     tmp.microseconds = 1000;
  126.     ConvertTimeToInt(tmp, &timer_IntOneMillisecond);
  127.  
  128.     timer_IntZeroSeconds    = 0;
  129.     timer_IntOneMinute        = timer_IntOneSecond * 60;
  130.     timer_IntOneHour        = timer_IntOneSecond * 3600;
  131.  
  132.     bzero((Address)&timer_TicksZeroSeconds, sizeof(timer_TicksZeroSeconds));
  133. }
  134.  
  135. /*
  136.  *----------------------------------------------------------------------
  137.  *
  138.  *  Timer_AddTicks --
  139.  *
  140.  *     Adds two tick values together.
  141.  *    For the Sun-3, this routine is #defined to be Time_Add().
  142.  *
  143.  *
  144.  *  Results:
  145.  *    A time in ticks.
  146.  *
  147.  *  Side effects:
  148.  *    None.
  149.  *
  150.  * This is macro defined in timerTick.h
  151.  * #define Timer_AddTicks(a,b,c)        Time_Add(a,b,c)
  152.  *
  153.  *----------------------------------------------------------------------
  154.  */
  155.  
  156.  
  157. /*
  158.  *----------------------------------------------------------------------
  159.  *
  160.  *  Timer_SubtractTicks --
  161.  *
  162.  *     Subtracts the second parameter from the first parameter. 
  163.  *    The second parameter must be less than the first, otherwise 
  164.  *    a zero tick value is returned.
  165.  *    For the Sun-3, this routine is #defined to be Time_Subtract().
  166.  *
  167.  *  Results:
  168.  *    An absolute time in ticks.
  169.  *
  170.  *  Side effects:
  171.  *    None.
  172.  *
  173.  * This is macro defined in timerTick.h
  174.  * #define Timer_SubtractTicks(a,b,c)    Time_Subtract(a,b,c)
  175.  *----------------------------------------------------------------------
  176.  */
  177.  
  178.  
  179. /*
  180.  *----------------------------------------------------------------------
  181.  *
  182.  *  Timer_AddIntervalToTicks --
  183.  *
  184.  *     Adds an interval (32-bit value) to an absolute time (64-bit value).
  185.  *
  186.  *  Results:
  187.  *    An absolute time in ticks.
  188.  *
  189.  *  Side effects:
  190.  *    None.
  191.  *
  192.  *----------------------------------------------------------------------
  193.  */
  194.  
  195. void
  196. Timer_AddIntervalToTicks(absolute, interval, resultPtr)
  197.     Timer_Ticks        absolute;    /* Addend 1 */
  198.     unsigned int    interval;    /* Addend 2 */
  199.     Timer_Ticks        *resultPtr;    /* Sum */
  200. {
  201.     Time    tmp;
  202.     /*
  203.      * Since Timer_Ticks is a time value, convert the interval to a time
  204.      * and use Timer_Add. .
  205.      */
  206.  
  207.     ConvertIntToTime(interval, &tmp);
  208.     Time_Add(absolute, tmp, resultPtr);
  209.  
  210. }
  211.  
  212.  
  213. /*
  214.  *----------------------------------------------------------------------
  215.  *
  216.  *  Timer_TicksToTime --
  217.  *
  218.  *      Converts a Timer_Ticks value into a Time value.
  219.  *
  220.  *  Results:
  221.  *    A time value in Time format.
  222.  *
  223.  *  Side effects:
  224.  *    None.
  225.  *
  226.  *    For the Sun-3, this routine is #defined to be *timePtr = tick;
  227.  * #define Timer_TicksToTime(a,b)        *(b) = a;
  228.  *----------------------------------------------------------------------
  229.  */
  230.  
  231.  
  232. /*
  233.  *----------------------------------------------------------------------
  234.  *
  235.  *  Timer_TimeToTicks --
  236.  *
  237.  *      Converts a Time value into a Timer_Ticks value.
  238.  *
  239.  *
  240.  *  Results:
  241.  *    A time value in ticks.
  242.  *
  243.  *  Side effects:
  244.  *    None.
  245.  *
  246.  *    For the Sun-3, this routine is #defined to be *ticksPtr = time;
  247.  *
  248.  *  #define Timer_TimeToTicks(a,b)        *(b) = a;
  249.  *
  250.  *----------------------------------------------------------------------
  251.  */
  252.  
  253.  
  254. /*
  255.  *----------------------------------------------------------------------
  256.  *
  257.  *  ConvertTimeToInt --
  258.  *
  259.  *      Converts a standard time value into a 32-bit interval value.
  260.  *
  261.  *  Results:
  262.  *    An interval value.
  263.  *
  264.  *  Side Effects:
  265.  *    None.
  266.  *
  267.  *----------------------------------------------------------------------
  268.  */
  269.  
  270. static void
  271. ConvertTimeToInt(time, resultPtr)
  272.     Time time;
  273.     unsigned int *resultPtr;
  274. {
  275.     if (Time_LE(time, maxIntervalTime)) {
  276.     *resultPtr = (time.seconds * ONE_SEC_INTERVAL) + 
  277.          ((time.microseconds * ONE_SEC_INTERVAL) / ONE_SECOND);
  278.     } else {
  279.     printf( "ConvertTimeToInt: time value too large\n");
  280.     *resultPtr = 0xFFFFFFFF;
  281.     }
  282. }
  283.  
  284.  
  285. /*
  286.  *----------------------------------------------------------------------
  287.  *
  288.  *  Timer_TicksToInterval --
  289.  *
  290.  *      Converts a ticks value into a 32-bit interval value.
  291.  *
  292.  *  Results:
  293.  *    An interval value.
  294.  *
  295.  *  Side Effects:
  296.  *    None.
  297.  *
  298.  *----------------------------------------------------------------------
  299.  */
  300. void
  301. Timer_TicksToInterval(ticks, resultPtr)
  302.     Timer_Ticks ticks;
  303.     unsigned int *resultPtr;
  304. {
  305.     ConvertTimeToInt(ticks, resultPtr);
  306. }
  307.  
  308.  
  309. /*
  310.  *----------------------------------------------------------------------
  311.  *
  312.  *  ConvertIntToTime --
  313.  *
  314.  *      Converts a 32-bit interval value into a standard time value.
  315.  *
  316.  *  Results:
  317.  *    A time value.
  318.  *
  319.  *  Side Effects:
  320.  *    None.
  321.  *
  322.  *----------------------------------------------------------------------
  323.  */
  324.  
  325. static void
  326. ConvertIntToTime(counter, resultPtr)
  327.     unsigned int counter;
  328.     Time *resultPtr;
  329. {
  330.     resultPtr->seconds = counter / ONE_SEC_INTERVAL;
  331.     resultPtr->microseconds = (counter % ONE_SEC_INTERVAL) * 
  332.                 (ONE_SECOND/ONE_SEC_INTERVAL);
  333. }
  334.  
  335.